﻿// Notepad+
// (C) 2014 Jan Kowalewicz <kowalewicz.jan@live.de>
//
// MainWindow.cs
// This file is part of the Notepad+ Project
//
// THIS PROJECT IS PUBLISHED UNDER THE TERMS OF THE LGPLv3

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using notepadplus.Dialogs;
using notepadplus.Dialogs.Updater;
using notepadplus.Dialogs.LocalUpdater;
using notepadplus.Properties;
using notepadplus.Dialogs.UserEditor;
using notepadplus.VirtualHTML_Environment.DeviceManager;
using notepadplus.Dialogs.Execute;
using notepadplus.Classes.TabManager;
using notepadplus.Classes.Editor;
using notepadplus.Dialogs.NewDocument;
using notepadplus.FormalSettings;

namespace notepadplus
{
    public partial class MainWindow : Form
    {
        Point position = new Point();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void subAboutItem_Click(object sender, EventArgs e)
        {
            AboutWindow abwin = new AboutWindow();
            abwin.Show(this);
        }

        private void subUpdaterPrefsItem_Click(object sender, EventArgs e)
        {
            UpdaterPrefsWindow upwin = new UpdaterPrefsWindow();
            upwin.Show(this);
        }

        private void subInstallLocalUpdateItem_Click(object sender, EventArgs e)
        {
            localUpdateSearch.ShowDialog(); // TODO: give the localInstaller the cookies!
        }

        private void MainWindow_Load(object sender, EventArgs e)
        {

            var tmanager = new TabsWorkflow();
            tmanager.addpage("", "new 0", manager);

            this.Text = "new 0 - Editor++";
            // Test if this the first run
            if (Settings.Default.isFirstRun == false)
            {
                // TODO: first run
            }
            else
            {
                // Load from settings
                this.Width = Settings.Default.WindowSizeWIDTH;
                this.Height = Settings.Default.WindowSizeHEIGTH;
                changeLangItem.Text = Settings.Default.StatusBarContextMenu_Header_Language;
                changeEncodingItem.Text = Settings.Default.StatusBarContextMenu_Header_Encoding;
                changeEOLItem.Text = Settings.Default.StatusBarContextMenu_Header_EOL;
                changeModeItem.Text = Settings.Default.StatusBarContextMenu_Header_EditMode;
                emCutItem.Text = Settings.Default.EditorContextMenu_Header_Cut;
                emCopyItem.Text = Settings.Default.EditorContextMenu_Header_Copy;
                emPasteItem.Text = Settings.Default.EditorContextMenu_header_Paste;
                emInsertItem.Text = Settings.Default.EditorContextMenu_Header_Insert;
                emDeleteItem.Text = Settings.Default.EditorContextMenu_Header_Delete;
                emSAItem.Text = Settings.Default.EditorContextMenu_Header_SelectAll;
                emSelRuntimeItem.Text = Settings.Default.EditorContextMenu_Header_RuntimeEnv;
                if (Settings.Default.keywordPanel_UnhideIn_Menu == false)
                {
                    subKeywordPanelItem.Visible = false;
                }
                else
                {
                    subKeywordPanelItem.Visible = true;
                }
                // ==== END ====

            }
            // ==== END ====
        }

        private void statusBar_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                statusBarMenu.Show(statusBar, new Point(e.X, e.Y));
            }
        }

        private void menuItem27_Click(object sender, EventArgs e)
        {
            StatusBarContextmenu editor = new StatusBarContextmenu();
            editor.Show(this);
        }

        private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Save window size
            Settings.Default.WindowSizeWIDTH = this.Width;
            Settings.Default.WindowSizeHEIGTH = this.Height;
            Settings.Default.Save();
            // ==== END ====
        }

        private void menuItem203_Click(object sender, EventArgs e)
        {
            mainwindow virtual_device_manager = new mainwindow();
            System.Threading.Thread.Sleep(1000);
            virtual_device_manager.Show(this);
        }

        private void executeWithProtocol_Click(object sender, EventArgs e)
        {
            ExecWithProto executewp = new ExecWithProto();
            executewp.Show(this);
        }

        private void manager_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Text = manager.SelectedTab.Text + " - Editor++";
        }

        private void manager_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                tabsMenu.Show(manager, new Point(e.X, e.Y));
            }
        }

        private void functionList_MouseClick(object sender, MouseEventArgs e)
        {
            //if (e.Button == System.Windows.Forms.MouseButtons.Right)
            //{
              //  functionlistMenu.Show(label1, new Point(e.X, e.Y));
            //}
        }

        private void subNewItem_Click(object sender, EventArgs e)
        {
            NdWindow newdoc = new NdWindow();
            newdoc.Show(this);
        }

        private void keywordPanel_MouseMove(object sender, MouseEventArgs e)
        {
            Panel p = sender as Panel;

            if (e.Button == MouseButtons.Left)
            {
                Point mousePos = this.PointToClient(Control.MousePosition);
                if (position.IsEmpty)
                    position = new Point(mousePos.X - p.Left, mousePos.Y - p.Top);

                p.Location = new Point(mousePos.X - position.X, mousePos.Y - position.Y);
            }
            else if (!position.IsEmpty)
                position = new Point();
        }

        private void menuItem113_Click(object sender, EventArgs e)
        {
            keywordPanel.Visible = true;
        }

        private void closeKeywordPanel_Click(object sender, EventArgs e)
        {
            keywordPanel.Visible = false;
        }

        private void keywordPanelOptions_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            KeywordPanelOptions kpop = new KeywordPanelOptions();
            kpop.Show(this);
        }

        private void settingsAutoRef_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Editor++ is now refreshing the Settings. This may take seconds. Don't click anywhere during this period!", "AutoRefresh", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            try
            {
                this.Width = Settings.Default.WindowSizeWIDTH;
            this.Height = Settings.Default.WindowSizeHEIGTH;
            changeLangItem.Text = Settings.Default.StatusBarContextMenu_Header_Language;
            changeEncodingItem.Text = Settings.Default.StatusBarContextMenu_Header_Encoding;
            changeEOLItem.Text = Settings.Default.StatusBarContextMenu_Header_EOL;
            changeModeItem.Text = Settings.Default.StatusBarContextMenu_Header_EditMode;
            emCutItem.Text = Settings.Default.EditorContextMenu_Header_Cut;
            emCopyItem.Text = Settings.Default.EditorContextMenu_Header_Copy;
            emPasteItem.Text = Settings.Default.EditorContextMenu_header_Paste;
            emInsertItem.Text = Settings.Default.EditorContextMenu_Header_Insert;
            emDeleteItem.Text = Settings.Default.EditorContextMenu_Header_Delete;
            emSAItem.Text = Settings.Default.EditorContextMenu_Header_SelectAll;
            emSelRuntimeItem.Text = Settings.Default.EditorContextMenu_Header_RuntimeEnv;
            if (Settings.Default.keywordPanel_UnhideIn_Menu == false)
            {
                subKeywordPanelItem.Visible = false;
            }
            else
            {
                subKeywordPanelItem.Visible = true;
            }
            MessageBox.Show("Successfully refreshed settings and Editor++!", "AutoRefresh", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception)
            {
                MessageBox.Show("Error by trying auto-refresh on 'notepadplus.MainWindow.settingsAutoRef_Click'!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
           
        }

        private void subSettingsItem_Click(object sender, EventArgs e)
        {
            SettingsWindow settings = new SettingsWindow();
            settings.Show(this);
        }

        private void directNew_Click(object sender, EventArgs e)
        {
            var tmanager = new TabsWorkflow();
            tmanager.addpage("", "new file", manager);
        }

       
    }
}
